package com.cx.bank.servlet.admin;

import com.cx.bank.handler.MyInVocationHandler;
import com.cx.bank.manager.IManager;
import com.cx.bank.manager.ManagerImpl;
import com.cx.bank.pojo.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

/**
 * 新增user
 * @author jxau_scf
 * @date 2021/8/5 - 9:27
 */
@WebServlet(urlPatterns = "/user/addUser")
public class AddUserServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doGet(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String userName = req.getParameter("userName");
        String userPassword = req.getParameter("userPassword");
        String userFlag = req.getParameter("userFlag");
        String userType = req.getParameter("userType");
        String balance = req.getParameter("balance");
        //获取代理对象，方便使用日志
        IManager manager = new ManagerImpl();
        MyInVocationHandler handler = new MyInVocationHandler();
        handler.setUserName(userName);
        handler.setTarget(manager);
        IManager proxy = (IManager)handler.getProxy();
        try {
            //添加用户
            boolean registerStatus = proxy.register(new User(userName, userPassword, Integer.valueOf(userFlag), userType, Double.valueOf(balance)));
            if (registerStatus){
                List<User> userList = proxy.findAllUser();
                System.out.println(userList.toString());
                req.setAttribute("userlist", userList);
                //服务端跳转（转发）
                req.getRequestDispatcher("/WEB-INF/jsp/user/admin/allUser.jsp").forward(req, resp);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

        super.doPost(req, resp);
    }
}
